package org.lq.er.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import org.lq.er.dao.SubjectDao;
import org.lq.er.entity.Exam_information;
import org.lq.er.entity.Subject;
import org.lq.er.util.DateUtil;
import org.lq.er.util.Instantiation;
import org.lq.er.util.JDBCUtil;

/**
 * 	科目类数据访问层实现类
 * @author 六组刘明昕
 * @date 2020-8-18 22:35:35
 */
public class SubjectDaoImpl implements SubjectDao,Instantiation<Subject> {
	/**
	 *  添加科目
	 */
	@Override
	public int save(Subject t) {
		int num = 0;
		String sql = "insert into subject(subject_name,start_date,end_date,begin_time,duration,cost)"
				+ "values(?,?,?,?,?,?)";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getSubjectName(),t.getStartDate(),t.getEndDate(),t.getBeginTime(),t.getDuration(),t.getCost());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return num;
	}
	/**
	 * 	修改科目
	 */
	@Override
	public int update(Subject t) {
		int num = 0;
		String sql = "update subject set subject_name = ?,start_date = ? ,end_date = ?,begin_time = ?,duration = ?,cost = ? where id = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getSubjectName(),t.getStartDate(),t.getEndDate(),t.getBeginTime(),t.getDuration(),t.getCost(),t.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 	根据id删除科目
	 */
	@Override
	public int delete(int id) {
		int num = 0;
		String sql = "delete from subject where id = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 	查询所有科目
	 */
	@Override
	public List<Subject> findAll() {
		List<Subject> list = new ArrayList<Subject>();
		String sql = "select * from subject";
		try {
			list = JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 	根据id查询科目
	 */
	@Override
	public Subject getById(int id) {
		System.out.println(id);
		List<Subject> list = new ArrayList<Subject>();
		String sql = "select * from subject where id = ?";
		try {
			list = JDBCUtil.executeQuery(sql, this,id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list.get(0);
	}

	@Override
	public Subject instance(ResultSet rs) {
		Subject s = new Subject();
		try {
			s.setId(rs.getInt("id"));
			s.setSubjectName(rs.getString("subject_name"));
			s.setStartDate(rs.getDate("start_date"));
			s.setEndDate(rs.getDate("end_date"));
			s.setBeginTime(DateUtil.getDate(rs.getString("begin_time")));
//			s.setDetailTime(rs.getString("detail_time"));
			s.setDuration(rs.getInt("duration"));
			s.setCost(rs.getInt("cost"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return s;
	}

	@Override
	public Subject getBySubjectName(String subjectName) {
		List<Subject> list = new ArrayList<Subject>();
		String sql = "select * from subject where subject_name = ?";
		try {
			list = JDBCUtil.executeQuery(sql, this,subjectName);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list.get(0);
	}
}
